<template>
  <!-- eslint-disable vue/html-self-closing -->
  <div>
    <el-card style="margin: 20px 0">
      <CategorySelect :show="!isShowTable" @getCategoryId="getCategoryId"></CategorySelect>
    </el-card>
    <el-card>
      <div v-show="isShowTable">
        <el-button :disabled="!category3Id" icon="el-icon-plus" style="margin-bottom:10px" type="primary" @click="addAttr">添加属性
        </el-button>
        <el-table :data="attrList" border style="width: 100%">
          <el-table-column align="center" label="序号" type="index" width="80">
          </el-table-column>
          <el-table-column align="center" label="属性名称" prop="attrName" width="150">
          </el-table-column>
          <el-table-column align="center" label="属性值列表" prop="address">
            <template slot-scope="{row}">
              <el-tag v-for="attrValue in row.attrValueList" :key="attrValue.id" style="margin:0 5px;" type="success">
                {{ attrValue.valueName }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" prop="address" width="150">
            <template slot-scope="{row}">
              <el-button icon="el-icon-edit" size="mini" type="warning" @click="updateAttr(row)"></el-button>
              <el-button icon="el-icon-delete" size="mini" type="danger"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div v-show="!isShowTable">
        <el-form :inline="true" :model="attrInfo" label-width="80px">
          <el-form-item label="属性名">
            <el-input v-model="attrInfo.attrName" placeholder="请输入属性名"></el-input>
          </el-form-item>
        </el-form>
        <el-button :disabled="!attrInfo.attrName" icon="el-icon-plus" type="primary" @click="addAttrValue">添加属性值
        </el-button>
        <el-button @click="isShowTable=true">取消</el-button>
        <el-table :data="attrInfo.attrValueList" border style="width: 100%;margin:10px 0">
          <el-table-column align="center" label="序号" type="index" width="80"></el-table-column>
          <el-table-column align="center" label="属性值名称">
            <template slot-scope="{row,$index}">
              <el-input v-if="row.flag" :ref="$index" v-model="row.valueName" placeholder="请输入属性值名称"
                        @blur="toLook(row)" @keyup.native.enter="toLook(row)"
              ></el-input>
              <span v-else style="display:block;" @click="toEdit(row,$index)">{{ row.valueName }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="{row,$index}">
              <el-popconfirm :title="`确定删除${row.valueName}吗?`" @onConfirm="deleteAttrValue($index)">
                <el-button slot="reference" icon="el-icon-delete" type="danger"></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <el-button :disabled="!attrInfo.attrValueList.length" type="primary" @click="addOrUpdateAttr">保存</el-button>
        <el-button @click="isShowTable=true">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
/* eslint-disable spaced-comment */
/* eslint-disable eqeqeq */
/* eslint-disable comma-dangle */
/* eslint-disable quotes */
/* eslint-disable semi */
import cloneDeep from 'lodash/cloneDeep'

export default {
  name: 'Attr',
  data() {
    return {
      category1Id: '',
      category2Id: '',
      category3Id: '',
      //平台属性
      attrList: [],
      //控制table是否显示
      isShowTable: true,
      //收集新增属性、修改属性
      attrInfo: {
        attrName: '', //属性名
        attrValueList: [], //属性值,每一个属性值都是一个对象
        categoryId: 0, //3级分类的id
        categoryLevel: 3
      }
    }
  },
  methods: {
    //自定义事件，获取子组件传来的id
    getCategoryId(data) {
      //区分三级分类相应的id以及进行存储
      if (data.level == 1) {
        this.category1Id = data.categoryId
        this.category2Id = ''
        this.category3Id = ''
      } else if (data.level == 2) {
        this.category2Id = data.categoryId
        this.category3Id = ''
      } else {
        this.category3Id = data.categoryId
        //发请求获取平台属性数据
        this.getAttrList()
      }
    },
    //获取平台属性的数据，当三级分类都确定时，向服务器发请求
    async getAttrList() {
      await this.$API.attr
        .reqAttrList(this.category1Id, this.category2Id, this.category3Id)
        .then((result) => {
          if (result.code == 200) {
            this.attrList = result.data
          }
        })
    },
    //添加属性值
    addAttrValue() {
      this.attrInfo.attrValueList.push({
        attrId: this.attrInfo.id,
        valueName: '',
        flag: true
      })
      this.$nextTick(() => {
        this.$refs[this.attrInfo.attrValueList.length - 1].focus()
      })
    },
    //添加属性按钮的回调
    addAttr() {
      this.isShowTable = false
      this.attrInfo = {
        attrName: '',
        attrValueList: [],
        categoryId: this.category3Id,
        categoryLevel: 3
      }
    },
    //修改某一个属性
    updateAttr(row) {
      this.isShowTable = false
      this.attrInfo = cloneDeep(row)
      // this.attrInfo = JSON.parse(JSON.stringify(row)); //先转为json再变成对象实现深拷贝
      this.attrInfo.attrValueList.forEach((item) => {
        this.$set(item, 'flag', false)
      })
    },
    //失去焦点，切换为查看模式
    toLook(row) {
      if (row.valueName.trim() == '') {
        this.$message({
          message: '输入不能为空',
          type: 'error'
        })
        return
      }
      const isRepat = this.attrInfo.attrValueList.some((item) => {
        if (row != item) {
          return row.valueName == item.valueName
        }
      })
      if (isRepat) {
        this.$message({
          message: '该属性值已存在',
          type: 'error'
        })
        return
      }
      row.flag = false
    },
    //点击span,变为编辑模式
    toEdit(row, index) {
      row.flag = true
      this.$nextTick(() => {
        this.$refs[index].focus()
      })
    },
    //气泡确认框确定按钮的回调
    deleteAttrValue(index) {
      this.attrInfo.attrValueList.splice(index, 1)
    },
    //保存按钮,添加属性或修改属性的操作
    async addOrUpdateAttr() {
      this.attrInfo.attrValueList = this.attrInfo.attrValueList.filter(
        (item) => {
          if (item.valueName != '') {
            delete item.flag
            return true
          }
        }
      )
      await this.$API.attr.reqAddOrUpdateAttr(this.attrInfo).then(() => {
        this.isShowTable = true
        this.$message({
          message: '保存成功',
          type: 'success'
        })
        this.getAttrList()
      }).catch(() => {
        this.$message({
          message: '保存失败',
          type: 'error'
        })
      })
    }
  }
}
</script>

<style></style>
